package com.bailan.foreat.controller;


import com.bailan.foreat.entity.Seller;
import com.bailan.foreat.entity.Store;
import com.bailan.foreat.service.StoreService;
import com.bailan.foreat.utils.SellerUtils;
import com.bailan.foreat.vo.EsStore;
import com.bailan.foreat.vo.StoreVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Controller
@RequestMapping("/store")
public class StoreController {

    @Autowired
    StoreService storeService;

    @RequestMapping("insert")
    public String insert(Integer addressId,Store store,@RequestParam("chooseTypes[]") Integer[] chooseTypes,HttpServletRequest request){

        Seller currentSeller = SellerUtils.getCurrentSeller(request);
        store.setSellerId(currentSeller.getId());
        store.setStatus(0);
        storeService.insert(addressId,store,chooseTypes);
        return "myStore";
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer storeId){

        storeService.deleteStore(storeId);
        return "ok";

    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Integer storeId,Integer status){
        storeService.updateStore(storeId,status);
        return "ok";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Store> selectAll(Integer sellerId){

        List<Store> storeList = storeService.selectMyAll(sellerId);


        return storeList;
    }

    @RequestMapping("select")
    @ResponseBody
    public Page select(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "2") Integer pageSize
    ) {

        Page storePage = storeService.selectAll(currentPage,pageSize);
        return storePage;
    }

    @RequestMapping("currentSellerAndMyStore")
    @ResponseBody
    public Page currentSeller(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "2") Integer pageSize,
            HttpServletRequest request
    ) {
        Seller currentSeller = SellerUtils.getCurrentSeller(request);
        Integer sellerId=currentSeller.getId();
        Page myStorePage = storeService.selectMyStore(currentPage, pageSize, sellerId);

        System.out.println(myStorePage);
        return myStorePage;
    }

    @RequestMapping("toStoreMenu")
    public String toStoreMenu(Integer storeId, Model model) {
        model.addAttribute("storeId", storeId);
        return "storemenu";
    }

    @RequestMapping("queryDetails")
    @ResponseBody
    public StoreVo queryDetails(Integer storeId){
        StoreVo storeVo = storeService.queryDetails(storeId);
        return storeVo;
    }


}

